import React, { Component, PropTypes } from "react";
import { connect } from 'react-redux';
import { Affix, Spin } from 'antd'
import Condition from "components/condition";
import Bar from 'containers/loan-billing/slide/bar/index'
const { BtnPrint, ButtonPrintCertificate, BtnBack, BtnDelete, AuditApplicationDetail, LoanBtn, BtnSubmit, BtnRecall, MentionBtn } = Bar;
const { If, Else } = Condition;

@connect(mapStateToProps)
class SlideLoanBar extends Component {

  buttonRoleSwitch = this.checkFunctionProfiles(['er.disabled'], [[false, undefined]])
    && this.checkFunctionProfiles(['finance.audit.disabled'], [[false, undefined]]);

  showApproveButton = () => {
    const { info } = this.context, { loginUser } = this.props;
    const { approvalChain, approvalChains = [], status } = info, currentUserOid = loginUser.userOID;
    let showBottom = false;
    if (status !== 1002) {
      return showBottom;
    }
    approvalChains.push(approvalChain);
    approvalChains.forEach((approver) => {
      if (approver.approverOID === currentUserOid) {
        showBottom = true;
      } else {
        if (approver.proxyApproverOIDs) {
          ~approver.proxyApproverOIDs.indexOf(currentUserOid) && (showBottom = true);
        }
      }
    });
    return showBottom;
  };

  render() {
    const { info, form, pathname, applicationOID, isWaitForAudit, isAuditInApproveStatus, approverOID, status,
      close, getInfo } = this.context;
    const { customFormPropertyMap = {} } = form;
    const recallVisible = customFormPropertyMap.formWithdrawAuthority === '1' ||
      (customFormPropertyMap.formWithdrawAuthority === '2' && info.withdrawFlag === 'Y');
    const view = ~pathname.indexOf('finance-view-loan') || ~pathname.indexOf('pay-workbench');
    const audit = ~pathname.indexOf('finance-audit');
    const confirmPay = ~pathname.indexOf('confirm-payment');
    const approve = ~pathname.indexOf('approve');
    return (
      <div>
        {
          Object.keys(info).length ? <div>
            <If value={view}>
              <Affix offsetTop={0} className='bottom-bar'>
                <BtnPrint/>
                <ButtonPrintCertificate businessCode={info.businessCode} appCode={'CA_PRINT_DOCUMENT'}/>
                <BtnBack/>
              </Affix>
            </If>
            <Else>
              <If value={info.directCreate}>
                <Affix offsetTop={0} className='bottom-bar'>
                  <If value={applicationOID && info.status === 1001}>
                    <BtnDelete/>
                  </If>
                  <If value={!audit && !confirmPay && !approve &&
                  (info.status > 1002 ||
                    (info.status === 1002 && info.rejectType === 1000 && info.printButtonDisplay))}>
                    <BtnPrint/>
                  </If>
                  <BtnBack/>
                </Affix>
              </If>
              <Else>
                <If value={applicationOID}>
                  <Affix offsetBottom={0} className='bottom-bar'>
                    <BtnBack/>
                  </Affix>
                </If>
                <Else>
                  <If value={audit}>
                    {
                      this.buttonRoleSwitch ? (
                        <AuditApplicationDetail
                          info={info}
                          formOID={info.formOID}
                          status={info.status}
                          entityOID={info.loanBillOID}
                          entityType={3001}
                          companyOid={info.companyOID}
                          approvalHistory={info.approvalHistorys}
                          goBack={close}
                          isWaitForAudit={isWaitForAudit}
                          customFormPropertyMap={customFormPropertyMap}
                          isAuditInApproveStatus={isAuditInApproveStatus}
                        />
                      ) : (
                        <Affix offsetTop={0} className='bottom-bar'>
                          <BtnBack/>
                        </Affix>
                      )
                    }
                  </If>
                  <If value={approve}>
                    <Affix offsetTop={0} className='bottom-bar'>
                      <LoanBtn
                        info={info}
                        approving={status === 'approving' && (this.showApproveButton() || info.admin)}
                        formInfo={form}
                        close={close}
                        handleApproveMention={getInfo}
                        params={{
                          approverOID
                        }}
                      />
                    </Affix>
                  </If>
                  <If value={!audit && !confirmPay && !approve}>
                    <Affix offsetTop={0} className='bottom-bar'>
                      <If value={info.status !== 1001}>
                        <MentionBtn
                          info={info}
                          customFormPropertyMap={customFormPropertyMap}
                          goBack={getInfo}
                          entityType={3001}
                          entityOID={info.loanBillOID}
                        />
                      </If>
                      <If value={info.status === 1002 && info.rejectType === 1000}>
                        <If value={recallVisible}>
                          <BtnRecall/>
                        </If>
                        <If value={info.printButtonDisplay}>
                          <BtnPrint/>
                        </If>
                      </If>
                      <If value={status === 1001}>
                        <BtnSubmit/>
                        <BtnDelete/>
                      </If>
                      <If value={info.status > 1002 && info.status < 1008}>
                        <BtnPrint/>
                      </If>
                      <BtnBack/>
                    </Affix>
                  </If>
                </Else>
              </Else>
            </Else>
          </div> : <Spin/>
        }
      </div>
    )
  }

  static contextTypes = {
    info: PropTypes.object,
    form: PropTypes.object,
    pathname: PropTypes.string,
    isWaitForAudit: PropTypes.bool,
    isAuditInApproveStatus: PropTypes.bool,
    approverOID: PropTypes.string,
    status: PropTypes.string,
    close: PropTypes.func,
    getInfo: PropTypes.func
  }

}

function mapStateToProps(state) {
  return {
    loginUser: state.login.loginUser
  }
}

export default SlideLoanBar
